<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 财付通支付响应处理 (中介担保)
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: Mediate.php 82 2012-03-19 02:35:48Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL 3
 */
class Payment_Respond_Tenpay_Mediate extends Payment_Respond
{

    /**
     * 分析返回的结果
     *
     * @param  array  $params
     * @return array
     */
    public function parse(array $params)
    {
        if (isset($params['total_fee']))
        {
            $params['total_fee'] = number_format($params['total_fee'] / 100, 2, '.', '');
        }

        if (isset($params['trade_price']))
        {
            $params['trade_price'] = number_format($params['trade_price'] / 100, 2, '.', '');
        }

        if (isset($params['transport_fee']))
        {
            $params['transport_fee'] = number_format($params['transport_fee'] / 100, 2, '.', '');
        }

        return parent::parse($params);
    }

    /**
     * 获取订单号
     *
     * @return string
     */
    public function get_id()
    {
        return $this->_result['mch_vno'];
    }

    /**
     * 获取支付交易号
     *
     * @return string
     */
    public function get_payment_no()
    {
        return $this->_result['cft_tid'];
    }

    /**
     * 获取数据签名
     *
     * @return string
     */
    public function get_sign()
    {
        return $this->_result['sign'];
    }

    /**
     * 获取订单总金额
     *
     * @return float
     */
    public function get_amount()
    {
        return floatval($this->_result['total_fee']);
    }

    /**
     * 获取付款时间
     *
     * @return integer
     */
    public function get_time()
    {
        return time();
    }

    /**
     * 获取支付状态
     *
     * @return integer
     */
    public function get_status()
    {
        if ($this->_result['retcode'] == 0)
        {
            switch ($this->_result['status'])
            {
                case 1: // 初始化
                    return Payment::STAT_INITED;
                case 2: // 等待付款
                    return Payment::STAT_CONFIRMED;
                case 3: // 已收款，等待发货
                    return Payment::STAT_PAID;
                case 4: // 已发货
                    return Payment::STAT_SHIPPED;
                case 5: // 已收货
                    return Payment::STAT_RECEIVED;
                case 6: // 超时
                    return Payment::STAT_TIMEOUT;
            }
        }

        return Payment::STAT_FAILED;
    }

    /**
     * 验证数字签名
     *
     * @return boolean
     */
    protected function _verifySign()
    {
        $args = empty($_GET) ? $_POST : $_GET;
        $sign = '';

        foreach ($args as $key => $value)
        {
            if ($key != 'sign' AND $value != '')
            {
                $sign .= ($sign == '' ? '' : '&')."$key=$value";
            }
        }

        $sign = strtoupper(md5("$sign&key=" . $this->_key));

        if ($sign != $this->get_sign())
        {
            throw new Payment_Exception('数字签名认证失败');
        }
    }

}